package net.suncaper.hotel.web.controller;

import net.suncaper.hotel.common.framework.RequestMapping;
import net.suncaper.hotel.common.utils.JSONUtil;
import net.suncaper.hotel.common.utils.PageInfo;
import net.suncaper.hotel.common.utils.Result;
import net.suncaper.hotel.model.dto.ReservationDTO;
import net.suncaper.hotel.model.vo.ReservationVO;
import net.suncaper.hotel.service.IReservationService;
import net.suncaper.hotel.service.impl.ReservationServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.util.List;

/**
 * 预订控制器
 */
@RequestMapping("/api/reservation")
public class ReservationController {
    private IReservationService reservationService = new ReservationServiceImpl();

    /**
     * 获取预订列表
     */
    @RequestMapping("/list")
    public void getReservationList(HttpServletRequest request, HttpServletResponse response) {
        try {
            String pageNumStr = request.getParameter("pageNum");
            String pageSizeStr = request.getParameter("pageSize");
            
            int pageNum = pageNumStr != null ? Integer.parseInt(pageNumStr) : 1;
            int pageSize = pageSizeStr != null ? Integer.parseInt(pageSizeStr) : 10;
            
            PageInfo<ReservationVO> pageInfo = reservationService.getReservationList(pageNum, pageSize);
            JSONUtil.printByJSON(response, Result.success(pageInfo));
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("获取预订列表失败"));
        }
    }

    /**
     * 根据状态获取预订
     */
    @RequestMapping("/listByStatus")
    public void getReservationsByStatus(HttpServletRequest request, HttpServletResponse response) {
        try {
            String status = request.getParameter("status");
            List<ReservationVO> list = reservationService.getReservationsByStatus(status);
            JSONUtil.printByJSON(response, Result.success(list));
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("获取预订列表失败"));
        }
    }

    /**
     * 获取今日入住
     */
    @RequestMapping("/todayCheckIns")
    public void getTodayCheckIns(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<ReservationVO> list = reservationService.getTodayCheckIns();
            JSONUtil.printByJSON(response, Result.success(list));
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("获取今日入住失败"));
        }
    }

    /**
     * 获取预订详情
     */
    @RequestMapping("/detail/*")
    public void getReservationDetail(HttpServletRequest request, HttpServletResponse response) {
        try {
            String uri = request.getRequestURI();
            String idStr = uri.substring(uri.lastIndexOf("/") + 1);
            if (idStr.endsWith(".do")) {
                idStr = idStr.substring(0, idStr.length() - 3);
            }
            
            Integer id = Integer.parseInt(idStr);
            ReservationVO vo = reservationService.getReservationById(id);
            
            if (vo != null) {
                JSONUtil.printByJSON(response, Result.success(vo));
            } else {
                JSONUtil.printByJSON(response, Result.fail("预订不存在"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("获取预订详情失败"));
        }
    }

    /**
     * 创建预订
     */
    @RequestMapping("/create")
    public void createReservation(HttpServletRequest request, HttpServletResponse response) {
        try {
            BufferedReader reader = request.getReader();
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            
            ReservationDTO dto = JSONUtil.fromJSON(sb.toString(), ReservationDTO.class);
            String reservationNo = reservationService.createReservation(dto);
            
            if (reservationNo != null) {
                JSONUtil.printByJSON(response, Result.success("创建预订成功", reservationNo));
            } else {
                JSONUtil.printByJSON(response, Result.fail("创建预订失败，房间可能不可用"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("创建预订失败"));
        }
    }

    /**
     * 更新预订
     */
    @RequestMapping("/update")
    public void updateReservation(HttpServletRequest request, HttpServletResponse response) {
        try {
            BufferedReader reader = request.getReader();
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            
            ReservationDTO dto = JSONUtil.fromJSON(sb.toString(), ReservationDTO.class);
            boolean success = reservationService.updateReservation(dto);
            
            if (success) {
                JSONUtil.printByJSON(response, Result.success("更新预订成功"));
            } else {
                JSONUtil.printByJSON(response, Result.fail("更新预订失败"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("更新预订失败"));
        }
    }

    /**
     * 确认预订
     */
    @RequestMapping("/confirm/*")
    public void confirmReservation(HttpServletRequest request, HttpServletResponse response) {
        try {
            String uri = request.getRequestURI();
            String idStr = uri.substring(uri.lastIndexOf("/") + 1);
            if (idStr.endsWith(".do")) {
                idStr = idStr.substring(0, idStr.length() - 3);
            }
            
            Integer id = Integer.parseInt(idStr);
            boolean success = reservationService.confirmReservation(id);
            
            if (success) {
                JSONUtil.printByJSON(response, Result.success("确认预订成功"));
            } else {
                JSONUtil.printByJSON(response, Result.fail("确认预订失败"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("确认预订失败"));
        }
    }

    /**
     * 取消预订
     */
    @RequestMapping("/cancel/*")
    public void cancelReservation(HttpServletRequest request, HttpServletResponse response) {
        try {
            String uri = request.getRequestURI();
            String idStr = uri.substring(uri.lastIndexOf("/") + 1);
            if (idStr.endsWith(".do")) {
                idStr = idStr.substring(0, idStr.length() - 3);
            }
            
            Integer id = Integer.parseInt(idStr);
            boolean success = reservationService.cancelReservation(id);
            
            if (success) {
                JSONUtil.printByJSON(response, Result.success("取消预订成功"));
            } else {
                JSONUtil.printByJSON(response, Result.fail("取消预订失败"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("取消预订失败"));
        }
    }
}